<?php

if($noview) { return; }

echo $html->div("statistics");
echo '<h2>'.__('Players Statistics', true).'</h2>';

$paginator->options(array('url' => $this->passedArgs));
$options = array('url' => array(
	'sort' => null,
	'direction' => null,
	'page' => empty($this->passedArgs['page']) ? 1 : $this->passedArgs['page']
));

$fields = array(
	'Player.strName' => array(
		'title' => __('Player', true),
		'description' => __('Player', true),
		'direction' => 'asc'),
	'Team.strName' => array(
		'title' => __('Team', true),
		'description' => __('Team', true),
		'direction' => 'asc'),
	'Race.ID' => array(
		'title' => __('Race', true),
		'description' => __('Race', true),
		'direction' => 'none'),
	'Coach.ID' => array(
		'title' => __('Coach', true),
		'description' => __('Coach', true),
		'direction' => 'none'),
	'PlayerType.ID' => array(
		'title' => __('Type', true),
		'description' => __('PlayerType', true),
		'direction' => 'none'),
	'StatisticsSeasonPlayer.iMatchPlayed' => array(
		'title' => __('GP', true),
		'description' => __('Game Played', true),
		'direction' => 'desc'),
	'StatisticsSeasonPlayer.iMVP' => array(
		'title' => __('MVP', true),
		'description' => __('Most Valuable Player', true),
		'direction' => 'desc'),
	'StatisticsSeasonPlayer.Inflicted_iPasses' => array(
		'title' => __('Pa', true),
		'description' => __('Passes', true),
		'direction' => 'desc'),
	'StatisticsSeasonPlayer.Inflicted_iCatches' => array(
		'title' => __('Ca', true),
		'description' => __('Catches', true),
		'direction' => 'desc'),
	'StatisticsSeasonPlayer.Inflicted_iInterceptions' => array(
		'title' => __('Int', true),
		'description' => __('Interceptions', true),
		'direction' => 'desc'),
	'StatisticsSeasonPlayer.Inflicted_iTouchdowns' => array(
		'title' => __('Td', true),
		'description' => __('Touchdowns', true),
		'direction' => 'desc'),
	'StatisticsSeasonPlayer.Inflicted_iCasualties' => array(
		'title' => __('Cas', true),
		'description' => __('Casualties', true),
		'direction' => 'desc'),
	'StatisticsSeasonPlayer.Inflicted_iTackles' => array(
		'title' => __('Tac', true),
		'description' => __('Tackles', true),
		'direction' => 'desc'),
	'StatisticsSeasonPlayer.Inflicted_iKO' => array(
		'title' => __('KO', true),
		'description' => __('Catches', true),
		'direction' => 'desc'),
	'StatisticsSeasonPlayer.Inflicted_iStuns' => array(
		'title' => __('St', true),
		'description' => __('Stuns', true),
		'direction' => 'desc'),
	'StatisticsSeasonPlayer.Inflicted_iInjuries' => array(
		'title' => __('Inj', true),
		'description' => __('Injuries', true),
		'direction' => 'desc'),
	'StatisticsSeasonPlayer.Inflicted_iDead' => array(
		'title' => __('Dd', true),
		'description' => __('Dead', true),
		'direction' => 'desc'),
	'StatisticsSeasonPlayer.Inflicted_iMetersRunning' => array(
		'title' => __('MR', true),
		'description' => __('Meters Running', true),
		'direction' => 'desc'),
	'StatisticsSeasonPlayer.Inflicted_iMetersPassing' => array(
		'title' => __('MP', true),
		'description' => __('Meters Passing', true),
		'direction' => 'desc'),
	'StatisticsSeasonPlayer.Sustained_iInterceptions' => array(
		'title' => __('sInt', true),
		'description' => __('Sustained Interceptions', true),
		'direction' => 'desc'),
	'StatisticsSeasonPlayer.Sustained_iCasualties' => array(
		'title' => __('sCas', true),
		'description' => __('Sustained Casualties', true),
		'direction' => 'desc'),
	'StatisticsSeasonPlayer.Sustained_iTackles' => array(
		'title' => __('sTac', true),
		'description' => __('Sustained Tackles', true),
		'direction' => 'desc'),
	'StatisticsSeasonPlayer.Sustained_iKO' => array(
		'title' => __('sKO', true),
		'description' => __('Sustained KO', true),
		'direction' => 'desc'),
	'StatisticsSeasonPlayer.Sustained_iStuns' => array(
		'title' => __('sSt', true),
		'description' => __('Sustained Stuns', true),
		'direction' => 'desc'),
	'StatisticsSeasonPlayer.Sustained_iInjuries' => array(
		'title' => __('sInj', true),
		'description' => __('Sustained Injuries', true),
		'direction' => 'desc'),
	'StatisticsSeasonPlayer.Inflicted_fAvgPasses' => array(
		'title' => __('Pa/G', true),
		'description' => __('Passes by Game', true),
		'direction' => 'desc'),
	'StatisticsSeasonPlayer.Inflicted_fAvgCatches' => array(
		'title' => __('Ca/G', true),
		'description' => __('Catches by Game', true),
		'direction' => 'desc'),
	'StatisticsSeasonPlayer.Inflicted_fAvgInterceptions' => array(
		'title' => __('Int/G', true),
		'description' => __('Interceptions by Game', true),
		'direction' => 'desc'),
	'StatisticsSeasonPlayer.Inflicted_fAvgTouchdowns' => array(
		'title' => __('Td/G', true),
		'description' => __('Touchdowns by Game', true),
		'direction' => 'desc'),
	'StatisticsSeasonPlayer.Inflicted_fAvgCasualties' => array(
		'title' => __('Cas/G', true),
		'description' => __('Casualties by Game', true),
		'direction' => 'desc'),
	'StatisticsSeasonPlayer.Inflicted_fAvgTackles' => array(
		'title' => __('Tac/G', true),
		'description' => __('Tackles by Game', true),
		'direction' => 'desc'),
	'StatisticsSeasonPlayer.Inflicted_fAvgKO' => array(
		'title' => __('KO/G', true),
		'description' => __('Catches by Game', true),
		'direction' => 'desc'),
	'StatisticsSeasonPlayer.Inflicted_fAvgStuns' => array(
		'title' => __('St/G', true),
		'description' => __('Stuns by Game', true),
		'direction' => 'desc'),
	'StatisticsSeasonPlayer.Inflicted_fAvgInjuries' => array(
		'title' => __('Inj/G', true),
		'description' => __('Injuries by Game', true),
		'direction' => 'desc'),
	'StatisticsSeasonPlayer.Inflicted_fAvgDead' => array(
		'title' => __('Dd/G', true),
		'description' => __('Dead by Game', true),
		'direction' => 'desc'),
	'StatisticsSeasonPlayer.Inflicted_fAvgMetersRunning' => array(
		'title' => __('MR/G', true),
		'description' => __('Meters Running by Game', true),
		'direction' => 'desc'),
	'StatisticsSeasonPlayer.Inflicted_fAvgMetersPassing' => array(
		'title' => __('MP/G', true),
		'description' => __('Meters Passing by Game', true),
		'direction' => 'desc'),
	'StatisticsSeasonPlayer.Sustained_fAvgInterceptions' => array(
		'title' => __('sInt/G', true),
		'description' => __('Sustained Interceptions by Game', true),
		'direction' => 'desc'),
	'StatisticsSeasonPlayer.Sustained_fAvgCasualties' => array(
		'title' => __('sCas/G', true),
		'description' => __('Sustained Casualties by Game', true),
		'direction' => 'desc'),
	'StatisticsSeasonPlayer.Sustained_fAvgTackles' => array(
		'title' => __('sTac/G', true),
		'description' => __('Sustained Tackles by Game', true),
		'direction' => 'desc'),
	'StatisticsSeasonPlayer.Sustained_fAvgKO' => array(
		'title' => __('sKO/G', true),
		'description' => __('Sustained KO by Game', true),
		'direction' => 'desc'),
	'StatisticsSeasonPlayer.Sustained_fAvgStuns' => array(
		'title' => __('sSt/G', true),
		'description' => __('Sustained Stuns by Game', true),
		'direction' => 'desc'),
	'StatisticsSeasonPlayer.Sustained_fAvgInjuries' => array(
		'title' => __('sInj/G', true),
		'description' => __('Sustained Injuries by Game', true),
		'direction' => 'desc'),
);

$current = empty($this->passedArgs['sort'])?'StatisticsSeasonPlayer.Inflicted_iTouchdowns':$this->passedArgs['sort'];
$fields[$current]['direction'] = (empty($this->passedArgs['direction']) || $this->passedArgs['direction'] == 'desc')?'asc' : 'desc';

echo '<p>';
echo $paginator->counter(array(
	'format' => __('Page %page% of %pages%, showing %current% records out of %count% total, starting on record %start%, ending on %end%', true)
));
echo '</p>';

echo '<table cellpadding="0" cellspacing="0">';
echo '<tr>';
echo '<th>#</th>';
foreach ( $fields as $key => $value ) {
	$options['url']['sort'] = $key;
	$options['url']['direction'] = $fields[$key]['direction'];

	echo '<th><acronym title="'.$fields[$key]['description'].'">';
	if($fields[$key]['direction'] != 'none') {
		echo $paginator->sort($fields[$key]['title'], $key, $options);
	} else {
		echo $fields[$key]['title'];
	}
	echo '</acronym></th>';
}
echo '</tr>';

$i = 0;
foreach ($statisticsSeasonPlayers as $statisticsSeasonPlayer) {
	$class = null;
	if((!empty($statisticsSeasonPlayer['Player']['Team']['Coach']))
			&& $statisticsSeasonPlayer['Player']['Team']['Coach']['ID'] == $coach_id) {
		$class = ' class="mine';
	} else {
		$class = ' class="';
	}
	if ($i++ % 2 == 0) {
		$class .= ' altrow"';
	} else {
		$class .= '"';
	}

	echo '<tr '.$class.'>';
	echo '<td nowrap>'.$i.'</td>';
	echo '<td nowrap>'.$html->link($statisticsSeasonPlayer['Player']['strName'], array('controller' => 'players', 'action' => 'view', $statisticsSeasonPlayer['Player']['ID'])).'</td>';
	echo '<td nowrap>'.$html->link($statisticsSeasonPlayer['Player']['Team']['strName'], array('controller' => 'teams', 'action' => 'view', $statisticsSeasonPlayer['Player']['Team']['ID'])).'</td>';
	echo '<td nowrap>'.$html->link($statisticsSeasonPlayer['Player']['Team']['Race']['LocalizedString'][$locale], array('controller' => 'races', 'action' => 'view', $statisticsSeasonPlayer['Player']['Team']['Race']['ID'])).'</td>';
	if(!empty($statisticsSeasonPlayer['Player']['Team']['Coach'])) {
		echo '<td nowrap>'.$html->link($statisticsSeasonPlayer['Player']['Team']['Coach']['strName'], array('controller' => 'coaches', 'action' => 'view', $statisticsSeasonPlayer['Player']['Team']['Coach']['ID'])).'</td>';
	} else {
		echo '<td nowrap>Unknown</td>';
	}
	echo '<td nowrap>'.$html->link($statisticsSeasonPlayer['Player']['PlayerType']['LocalizedString'][$locale], array('controller' => 'player_types', 'action' => 'view', $statisticsSeasonPlayer['Player']['PlayerType']['ID'])).'</td>';
	foreach ( $statisticsSeasonPlayer['StatisticsSeasonPlayer'] as $key => $value ) {

		switch ( $key ) {
			case 'ID':
			case 'idPlayer_Listing':
			case 'Sustained_iDead':
			case 'iSeason':
			case 'bFinished':
			case 'idRule_Listing':
				break;
			default:
				if(ereg("fAvg", $key)) {
					$value = $number->precision($value,1);
				}
				echo '<td>'.$value.'</td>';
				break;
		}
	}
	echo '</tr>';
}
echo '</table>';
echo $html->div("paginating");
echo $paginator->prev('<< '.__('previous', true), array(), null, array('class'=>'disabled'));
echo ' | '.$paginator->numbers();
echo ' | ';
echo $paginator->next(__('next', true).' >>', array(), null, array('class' => 'disabled'));
echo '</div>';
echo '</div>';